package com.cskaoyan.kpi.etl;

import ip.QQWryExt;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * @ClassName ETLMapper
 * @Description: TODO
 * @Author 远志 zhangsong@cskaoyan.onaliyun.com
 * @Date 2023/2/14 14:30
 * @Version V1.0
 **/
public class ETLMapper extends Mapper<Object, Text, Text, NullWritable> {

    //这个一定要写成成员变量，否则会执行几天几夜
    QQWryExt ext;

    {
        try {
            ext = new QQWryExt();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void map(Object key, Text value, Context context) throws IOException, InterruptedException {
        //61.237.124.223---2022-01-14 00:00:00---http://115.29.141.32:8085/#/mall/goods/484---firefox
        String line = value.toString();
        // 0 ip、1、2、3
        String[] parts = line.split("---");
        String ip = parts[0];
        String datetime = parts[1];
        String url = parts[2];
        String device = parts[3];
        QQWryExt.RegionInfo regionInfo = ext.analyticIP(ip);
        //可以将这些数据重新拼接在一起  ip---regionInfo---time---url---device
        String region = regionInfo.getCountry() + ":" + regionInfo.getProvince() + ":" + regionInfo.getCity();
        String newLine = ip + "---" + region + "---" + datetime + "---" + url + "---" + device;
        context.write(new Text(newLine), NullWritable.get());
    }
}
